<?php
session_start();
if (isset($_SESSION["username"])) {
?>
  <?php require_once('common/header.php'); ?>
  <!--页面主要内容-->
  <main class="lyear-layout-content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-toolbar clearfix">
              <form class="pull-right search-bar" method="get" action="" role="form">
                <div class="input-group">
                  <div class="input-group-btn">
                    <input type="hidden" name="search" id="search" value="channel_name">
                    <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                      渠道名称 <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                      <li> <a tabindex="-1" href="javascript:void(0)" data-field="channel_name">渠道名称</a> </li>
                      <li> <a tabindex="-1" href="javascript:void(0)" data-field="channel_leader">负责人</a> </li>
                    </ul>
                  </div>
                  <input type="text" class="form-control" value="" name="keyword" placeholder="请输入名称">
                </div>
              </form>
              <div class="toolbar-btn-action">
                <a class="btn btn-primary m-r-5" href="addChannel.php"><i class="mdi mdi-plus"></i> 新增渠道</a>
              </div>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <form name="form1" id="form1" method="post" action="controller/deleteall.php">
                  <table class="table table-bordered">
                     <?php
                        include_once("../untils/conn.php");
                        mysqli_query($con, "set names utf8");
                        if ($con) {
                          if ($db) {
                            // 获取搜索关键字
                            $keyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
                            // 构建查询条件
                            $searchField = isset($_GET['search']) ? $_GET['search'] : 'channel_name';
                            $whereClause = '';
                            if (!empty($keyword)) {
                              $keyword = mysqli_real_escape_string($con, $keyword);
                              $whereClause = "WHERE $searchField LIKE '%$keyword%'";
                            }
                            // 获取数据总行数
                            $sql = "SELECT * FROM channel $whereClause";
                            $data = mysqli_query($con, $sql);
                            $maxrows = mysqli_num_rows($data);
                            // 计算总页数
                            $page_size = 10;  // 每页显示数
                            if ($maxrows % $page_size == 0) {
                              $maxpage = (int)($maxrows / $page_size);
                            } else {
                              $maxpage = (int)($maxrows / $page_size) + 1;
                            }
                            // 获取当前页
                            if (isset($_GET['curpage'])) {
                              $page = $_GET['curpage'];
                            } else {
                              $page = 1;
                            }
                            // 分段取出数据
                            $start = $page_size * ($page - 1);
                            $get_sql = "SELECT * FROM channel $whereClause LIMIT $start,$page_size";
                            // 得到数据显示
                            $data = mysqli_query($con, $get_sql);
                            $sqlyys = "SELECT * FROM `sort` ORDER BY `sortid` DESC LIMIT 1";
                            $yysdata = mysqli_query($con, $sqlyys);
                            $yys = mysqli_fetch_row($yysdata);
                            $index = $yys[1];
                        ?>
                        <thead>
                          <tr>
                            <th>渠道ID</th>
                            <th>渠道名称</th>
                            <th>渠道类型</th>
                            <th>负责人</th>
                            <th>电话</th>
                            <th>创建时间</th>
                           
                            
                            <th>操作</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php
                          while ($row = mysqli_fetch_array($data)) {
                          ?>
                            <tr>
                              <td style="width:5%;">
                                <?php echo $row["channel_id"] ?>
                              </td>
                              <td style="width:5%;">
                                <?php echo $row["channel_name"] ?>
                              </td>
                              
                              <td style="width:7.5%;">
                                <?php 
                                    switch ($row["channel_type"]) {
                                        case 0:
                                            echo "线上渠道";
                                            break;
                                        case 1:
                                            echo "线下渠道";
                                            break;
                                    } 
                                ?>
                            </td>
                              </td>
                              <td style="width:10%;"><?php echo $row["channel_leader"] ?>
                              </td>
                              
                              <td style="width:10%;"><?php echo $row["channel_phone"] ?>
                              </td>
                              
                              <td style="width:7.5%;">
                               <?php 
                               $timestamp= $row["channel_ctime"] ;
                               $date = date('Y-m-d H:i:s', $timestamp);
                                echo $date; // 输出：2021-07-22 16:52:07
                               ?>
                              </td>
                              

                              
                              <td style="width:15%;">
                                <a href="../channel.php?yys=<?php echo $index ?>&channel_id=<?php echo $row['channel_id'] ?> "class="btn .btn-warning btn-sm" target="_blank" >渠道主页</a>  
                                  
                                <a href="channellist.php?channel_id=<?php echo $row['channel_id'] ?> "class="btn btn-primary btn-sm" >渠道商品</a>
                                <a href="channeloder.php?channel_id=<?php echo $row['channel_id'] ?>" class="btn btn-info btn-sm">渠道订单</a>
                                <a href="upChannel.php?channel_id=<?php echo $row['channel_id'] ?> " class="btn btn-success btn-sm">修改</a>
                                <a href="controller/deletechannel.php?channel_id=<?php echo $row['channel_id'] ?>" onclick=" return del()" class="btn btn-danger btn-sm">删除</a>
                              </td>
                            </tr>
                          <?php
                          }
                          ?>
                        </tbody>
                        <tfoot>
                          <tr>
                            <td colspan="12" style="text-align: right;">
                          <?php
                          echo "<p>共 $maxpage 页&nbsp;&nbsp;";
                          echo "每页 $page_size 项&nbsp;&nbsp;";
                          //设置上一页
                          if ($page > 1) {
                            $prepage = $page - 1;
                            echo "<a href='?curpage=$prepage'>上一页</a>&nbsp;&nbsp;";
                          }
                          //设置下一页
                          if ($page < $maxpage) {
                            $nextpage = $page + 1;
                            echo "<a href='?curpage=$nextpage'>下一页</a>&nbsp;&nbsp;";
                          }
                          echo "&nbsp;&nbsp;第 $page 页</p>";
                        }
                      }
                          ?>
                            </td>
                          </tr>
                        </tfoot>
                  </table>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    </div>
  </main>
   <script src="../merchant/assets/js/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
        $('.dropdown-menu a').click(function(e) {
          e.preventDefault();
          var field = $(this).data('field');
          var buttonText = $(this).text();
          $('#search').val(field);
          $('#search-btn').html(buttonText + ' <span class="caret"></span>');
        });
      });
    </script>
  <!--End 页面主要内容-->
  <?php require_once('common/footer.php'); ?>
<?php
} else {
  echo "<script>alert('您尚未登录，没有权限访问该页面');location.href='login.php';</script>";
}
